<?php
namespace App\Services;

use App\Models\ExhibitionCode;
use Illuminate\Support\Str;
class ExhibitionCodeService{
    /**
     * @return mixed 获取小程序签到页二维码
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     */
    public function getExhibitionCode(){
        $miniProgram = \EasyWeChat::miniProgram(); // 小程序
        $url_path = "pages/sign_in";
        $response = $miniProgram->app_code->get($url_path);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $img_name = time() . Str::random(3) . ".png";
            $filename = $response->saveAs(public_path() . '/uploads/', $img_name);
            $exhibitionCode =  ExhibitionCode::create(['img_url'=>'/uploads/'.$filename]);
            return $exhibitionCode;
        }
    }
}
